﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YuuzleComic.IDAL;
using YuuzleComic.DBUtility;//Please add references
namespace YuuzleComic.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:comics
    /// </summary>
    public partial class comics : Icomics
    {
        public comics()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "comics");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from comics");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该漫画名称
        /// </summary>
        public bool Exists(string mhmc)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from comics");
            strSql.Append(" where mhmc=@mhmc ");
            SqlParameter[] parameters = {
					new SqlParameter("@mhmc", SqlDbType.VarChar)};
            parameters[0].Value = mhmc;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YuuzleComic.Model.comics model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into comics(");
            strSql.Append("classid,mhmc,zzid,dqid,zm,zt,mhfm,hits_d,hits_d_d,hits_w,hits_w_d,hits_m,hits_m_d,hits,mhjs,adddate,tj,cjid,cjitemid,mhbm,mhtc,mhjj)");
            strSql.Append(" values (");
            strSql.Append("@classid,@mhmc,@zzid,@dqid,@zm,@zt,@mhfm,@hits_d,@hits_d_d,@hits_w,@hits_w_d,@hits_m,@hits_m_d,@hits,@mhjs,@adddate,@tj,@cjid,@cjitemid,@mhbm,@mhtc,@mhjj)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@classid", SqlDbType.Int,4),
					new SqlParameter("@mhmc", SqlDbType.VarChar,255),
					new SqlParameter("@zzid", SqlDbType.Int,4),
					new SqlParameter("@dqid", SqlDbType.Int,4),
					new SqlParameter("@zm", SqlDbType.VarChar,50),
					new SqlParameter("@zt", SqlDbType.Bit,1),
					new SqlParameter("@mhfm", SqlDbType.VarChar,255),
					new SqlParameter("@hits_d", SqlDbType.Int,4),
					new SqlParameter("@hits_d_d", SqlDbType.DateTime),
					new SqlParameter("@hits_w", SqlDbType.Int,4),
					new SqlParameter("@hits_w_d", SqlDbType.Int,4),
					new SqlParameter("@hits_m", SqlDbType.Int,4),
					new SqlParameter("@hits_m_d", SqlDbType.DateTime),
					new SqlParameter("@hits", SqlDbType.Int,4),
					new SqlParameter("@mhjs", SqlDbType.Text),
					new SqlParameter("@adddate", SqlDbType.DateTime),
					new SqlParameter("@tj", SqlDbType.Bit,1),
					new SqlParameter("@cjid", SqlDbType.Int,4),
					new SqlParameter("@cjitemid", SqlDbType.VarChar,50),
                    new SqlParameter("@mhbm", SqlDbType.VarChar,255),
                    new SqlParameter("@mhtc", SqlDbType.VarChar,255),
                    new SqlParameter("@mhjj", SqlDbType.VarChar,300)};
            parameters[0].Value = model.classid;
            parameters[1].Value = model.mhmc;
            parameters[2].Value = model.zzid;
            parameters[3].Value = model.dqid;
            parameters[4].Value = model.zm;
            parameters[5].Value = model.zt;
            parameters[6].Value = model.mhfm;
            parameters[7].Value = model.hits_d;
            parameters[8].Value = model.hits_d_d;
            parameters[9].Value = model.hits_w;
            parameters[10].Value = model.hits_w_d;
            parameters[11].Value = model.hits_m;
            parameters[12].Value = model.hits_m_d;
            parameters[13].Value = model.hits;
            parameters[14].Value = model.mhjs;
            parameters[15].Value = model.adddate;
            parameters[16].Value = model.tj;
            parameters[17].Value = model.cjid;
            parameters[18].Value = model.cjitemid;
            parameters[19].Value = model.mhbm;
            parameters[20].Value = model.mhtc;
            parameters[21].Value = model.mhjj;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(YuuzleComic.Model.comics model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update comics set ");
            strSql.Append("classid=@classid,");
            strSql.Append("mhmc=@mhmc,");
            strSql.Append("zzid=@zzid,");
            strSql.Append("dqid=@dqid,");
            strSql.Append("zm=@zm,");
            strSql.Append("zt=@zt,");
            strSql.Append("mhfm=@mhfm,");
            strSql.Append("hits_d=@hits_d,");
            strSql.Append("hits_d_d=@hits_d_d,");
            strSql.Append("hits_w=@hits_w,");
            strSql.Append("hits_w_d=@hits_w_d,");
            strSql.Append("hits_m=@hits_m,");
            strSql.Append("hits_m_d=@hits_m_d,");
            strSql.Append("hits=@hits,");
            strSql.Append("mhjs=@mhjs,");
            strSql.Append("adddate=@adddate,");
            strSql.Append("tj=@tj,");
            strSql.Append("cjid=@cjid,");
            strSql.Append("cjitemid=@cjitemid,");
            strSql.Append("mhbm=@mhbm,");
            strSql.Append("mhjj=@mhjj,");
            strSql.Append("mhtc=@mhtc");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@classid", SqlDbType.Int,4),
					new SqlParameter("@mhmc", SqlDbType.VarChar,255),
					new SqlParameter("@zzid", SqlDbType.Int,4),
					new SqlParameter("@dqid", SqlDbType.Int,4),
					new SqlParameter("@zm", SqlDbType.VarChar,50),
					new SqlParameter("@zt", SqlDbType.Bit,1),
					new SqlParameter("@mhfm", SqlDbType.VarChar,255),
                    new SqlParameter("@hits_d", SqlDbType.Int,4),
					new SqlParameter("@hits_d_d", SqlDbType.DateTime),
					new SqlParameter("@hits_w", SqlDbType.Int,4),
					new SqlParameter("@hits_w_d", SqlDbType.Int,4),
					new SqlParameter("@hits_m", SqlDbType.Int,4),
					new SqlParameter("@hits_m_d", SqlDbType.DateTime),
					new SqlParameter("@hits", SqlDbType.Int,4),
					new SqlParameter("@mhjs", SqlDbType.Text),
                    new SqlParameter("@adddate", SqlDbType.DateTime),
					new SqlParameter("@tj", SqlDbType.Bit,1),
					new SqlParameter("@cjid", SqlDbType.Int,4),
					new SqlParameter("@cjitemid", SqlDbType.VarChar,50),
                    new SqlParameter("@mhbm", SqlDbType.VarChar,255),
                    new SqlParameter("@mhjj", SqlDbType.VarChar,300),
                    new SqlParameter("@mhtc", SqlDbType.VarChar,255),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.classid;
            parameters[1].Value = model.mhmc;
            parameters[2].Value = model.zzid;
            parameters[3].Value = model.dqid;
            parameters[4].Value = model.zm;
            parameters[5].Value = model.zt;
            parameters[6].Value = model.mhfm;
            parameters[7].Value = model.hits_d;
            parameters[8].Value = model.hits_d_d;
            parameters[9].Value = model.hits_w;
            parameters[10].Value = model.hits_w_d;
            parameters[11].Value = model.hits_m;
            parameters[12].Value = model.hits_m_d;
            parameters[13].Value = model.hits;
            parameters[14].Value = model.mhjs;
            parameters[15].Value = model.adddate;
            parameters[16].Value = model.tj;
            parameters[17].Value = model.cjid;
            parameters[18].Value = model.cjitemid;
            parameters[19].Value = model.mhbm;
            parameters[20].Value = model.mhjj;
            parameters[21].Value = model.mhtc;
            parameters[22].Value = model.id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from comics ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from comics ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YuuzleComic.Model.comics GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,classid,mhmc,zzid,dqid,zm,zt,mhfm,hits_d,hits_d_d,hits_w,hits_w_d,hits_m,hits_m_d,hits,mhjs,adddate,tj,cjid,cjitemid,mhbm,mhtc from comics ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            YuuzleComic.Model.comics model = new YuuzleComic.Model.comics();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["classid"].ToString() != "")
                {
                    model.classid = int.Parse(ds.Tables[0].Rows[0]["classid"].ToString());
                }
                model.mhmc = ds.Tables[0].Rows[0]["mhmc"].ToString();
                if (ds.Tables[0].Rows[0]["zzid"].ToString() != "")
                {
                    model.zzid = int.Parse(ds.Tables[0].Rows[0]["zzid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["dqid"].ToString() != "")
                {
                    model.dqid = int.Parse(ds.Tables[0].Rows[0]["dqid"].ToString());
                }
                model.zm = ds.Tables[0].Rows[0]["zm"].ToString();
                if (ds.Tables[0].Rows[0]["zt"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["zt"].ToString() == "1") || (ds.Tables[0].Rows[0]["zt"].ToString().ToLower() == "true"))
                    {
                        model.zt = true;
                    }
                    else
                    {
                        model.zt = false;
                    }
                }
                model.mhfm = ds.Tables[0].Rows[0]["mhfm"].ToString();
                if (ds.Tables[0].Rows[0]["hits_d"].ToString() != "")
                {
                    model.hits_d = int.Parse(ds.Tables[0].Rows[0]["hits_d"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits_d_d"].ToString() != "")
                {
                    model.hits_d_d = DateTime.Parse(ds.Tables[0].Rows[0]["hits_d_d"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits_w"].ToString() != "")
                {
                    model.hits_w = int.Parse(ds.Tables[0].Rows[0]["hits_w"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits_w_d"].ToString() != "")
                {
                    model.hits_w_d = int.Parse(ds.Tables[0].Rows[0]["hits_w_d"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits_m"].ToString() != "")
                {
                    model.hits_m = int.Parse(ds.Tables[0].Rows[0]["hits_m"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits_m_d"].ToString() != "")
                {
                    model.hits_m_d = DateTime.Parse(ds.Tables[0].Rows[0]["hits_m_d"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits"].ToString() != "")
                {
                    model.hits = int.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
                }
                model.mhjs = ds.Tables[0].Rows[0]["mhjs"].ToString();
                if (ds.Tables[0].Rows[0]["adddate"].ToString() != "")
                {
                    model.adddate = DateTime.Parse(ds.Tables[0].Rows[0]["adddate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["tj"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["tj"].ToString() == "1") || (ds.Tables[0].Rows[0]["tj"].ToString().ToLower() == "true"))
                    {
                        model.tj = true;
                    }
                    else
                    {
                        model.tj = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["cjid"].ToString() != "")
                {
                    model.cjid = int.Parse(ds.Tables[0].Rows[0]["cjid"].ToString());
                }
                model.cjitemid = ds.Tables[0].Rows[0]["cjitemid"].ToString();
                model.mhbm = ds.Tables[0].Rows[0]["mhbm"].ToString();
                model.mhtc = ds.Tables[0].Rows[0]["mhtc"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,classid,mhmc,zzid,dqid,zm,zt,mhfm,hits_d,hits_d_d,hits_w,hits_w_d,hits_m,hits_m_d,hits,mhjs,adddate,tj,cjid,cjitemid,mhtc,mhbm ");
            strSql.Append(" FROM comics ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,classid,mhmc,zzid,dqid,zm,zt,mhfm,hits_d,hits_d_d,hits_w,hits_w_d,hits_m,hits_m_d,hits,mhjs,adddate,tj,cjid,cjitemid ");
            strSql.Append(" FROM comics ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "comics";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetViewList(string fldName,int isDesc,int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "View_Comic_List";
            parameters[1].Value = fldName;
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;
            parameters[5].Value = isDesc;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  Method
    }
}

